package view;

import java.util.ArrayList;
import java.util.List;

import data.VendorInfoBean;
import utils.UtilsBean;

public class VendorDeviceBean {
	public VendorDeviceBean(){
		fetchVendorList();
	}
	private static List<VendorInfoBean> readVendors(){
		String value = UtilsBean.getProperty("vendorCount", "0");
		try{
			int count = Integer.parseInt(value);
			List<VendorInfoBean> vl =new ArrayList<VendorInfoBean>();
			for (int i=1; i<=count; i++){
				String vendorid = UtilsBean.getProperty("vendor_"+i+"_id", "");
				String vendorname = UtilsBean.getProperty("vendor_"+i+"_name", "");
				String vendorClassid = UtilsBean.getProperty("vendor_"+i+"_classid", "");
				String ocxfile = UtilsBean.getProperty("vendor_"+i+"_ocxfile", "");
				String algfile = UtilsBean.getProperty("vendor_"+i+"_algfile", "");
				String flag = UtilsBean.getProperty("vendor_"+i+"_enable_get_temp_by_img", "");
				vl.add(new VendorInfoBean(vendorid, vendorname, 
						vendorClassid, ocxfile, algfile, flag));
			}
			return vl;
		}
		catch(Exception ex){
			return new ArrayList<VendorInfoBean>();
		}
	}
	
	@SuppressWarnings("unchecked")
	public static List<VendorInfoBean> fetchVendorList(){
		List<VendorInfoBean> vl = (List<VendorInfoBean>)UtilsBean.getSessionObj("vendorList");
		if (vl == null){
			vl = readVendors();
			UtilsBean.setSessionObj("vendorList", vl);
		}
		return vl;
	}
	
	public static VendorInfoBean findVendor(String vendorid){
		for (VendorInfoBean v: fetchVendorList()){
			if (vendorid.equals(v.getVendorid())){
				return v;
			}
		}
		return null;
	}
	
	public List<VendorInfoBean> getVendorList(){
		return fetchVendorList();
	}
	public void setVendorList(List<VendorInfoBean> v){
		
	}
}
